package com.roc.web.action.dailiReport;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.MerchantReport;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.service.IPagenitionService;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
@Component
@Scope("prototype")
@Namespace("/dailiMonth")
public class DailiMonthReportAction  extends PaginationBaseAction {
private Logger logger=LogManager.getLogger(DailiMonthReportAction.class);
	
	private DailiMonthReportSearchCondition searchCondition;
	
	@Autowired
	IPagenitionService pagenitionService;
	@Autowired
	IorgService orgService;
	
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/daiLiReport/daiLiMonthQury.jsp")
    })
	public String init(){
		return SUCCESS;
	}
	
	@Action(value = "query", 
			results = { @Result(name = "success", location = "/pages/daiLiReport/daiLiMonthQury.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String query(){
		Operators loginOperators = (Operators) ActionContext.getContext().getSession().get(Constant.SESSION_LOGIN_MERCHANT);
		
		if(searchCondition==null){
			searchCondition=(DailiMonthReportSearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new DailiMonthReportSearchCondition();
			}
		}
		
		if(loginOperators.getRoleType().equals("03")){
			searchCondition.setAccountNo(loginOperators.getAccountNo());
		}else if(loginOperators.getRoleType().equals("02")){
			searchCondition.setOrgCode(loginOperators.getOrgCode());
		}
		
		try {
			
			init();
			if(Constance.Null2EmptyTrim(searchCondition.getDate()).equals("")){
				getRequest().setAttribute("msg", "请选择时间");
				
				return SUCCESS;
				
			}
			int rows=pagenitionService.count(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAll(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				
			}
			List<MerchantReport> merList=dealWidth(searchRsList);
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute("merList", merList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		}catch(ServiceException e){
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	
	private List<MerchantReport> dealWidth(List<Object> list) throws ServiceException{
		List<MerchantReport> merList=new ArrayList<MerchantReport>();
	    List<OrgBean> orgList=new ArrayList<OrgBean>();
		orgList=orgService.listOrgs((Operators) ActionContext.getContext().getSession().get(Constant.SESSION_LOGIN_MERCHANT));
		
        if(list.size()>0 && list!=null){
			
			Iterator iterator=list.iterator();
			while (iterator.hasNext()) {
				Object[] obj= (Object[])iterator.next();
				MerchantReport mer=new MerchantReport();
				mer.setAccountNo(Constance.Null2EmptyTrim(obj[0]));
				mer.setRealName(Constance.Null2EmptyTrim(obj[1]));
				mer.setOrgCode(Constance.Null2EmptyTrim(obj[2]));
				for(int i=0;i<orgList.size();i++){
					if(orgList.get(i).getOrg_code().endsWith(mer.getOrgCode())){
						mer.setOrgCode(orgList.get(i).getOrg_name());
						break;
					}
				}
				mer.setMccType(Constance.Null2EmptyTrim(obj[3]));
				mer.setTotalFee((Double.parseDouble(Constance.null2Amt(obj[4]))));
				mer.setTotalAmt((Double.parseDouble(Constance.null2Amt(obj[5]))));
				mer.setTotalNum(Long.parseLong(Constance.Null2EmptyTrim(obj[6])));
				mer.setTransDate(Constance.Null2EmptyTrim(obj[7]));
				merList.add(mer);
			}
        }
		
		return merList;
		
	} 
	
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return this.searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		// TODO Auto-generated method stub
		this.searchCondition=(DailiMonthReportSearchCondition) searchCondition;
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		// TODO Auto-generated method stub
		getSession().setAttribute(SEARCH_SESSION_DAILI_MONTH, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_DAILI_MONTH);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return null;
	}

	public DailiMonthReportSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(DailiMonthReportSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

}
